package org.water.common;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
 * HTTP工具类
 * @author qzy
 *
 */
import java.util.logging.Logger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HttpUtils {

	private static Log log=LogFactory.getLog(HttpUtils.class);
	/**
	 * ping ip 地址，检查服务器是否接通，需要服务器授权ping服务
	 * @param ip 服务器ip地址   格式127.0.0.1  默认10s的延迟
	 * @param waitTime 允许等待时间，建议10000即10s
	 * @return
	 */
	public boolean pingIp(String ip,int waitTime)
	{
		int timeout=waitTime;
		boolean r=false;
		try {
			r=InetAddress.getByName(ip).isReachable(timeout);
			log.info(ip+" available:"+r);
		} catch (UnknownHostException e) {
			log.error(ip+" 未知的主机地址");
		} catch (IOException e) {
			log.error(ip+" "+e.getMessage());
		}
		return r;
	}
	/**
	 * ping ip 地址，检查服务器是否接通，需要服务器授权ping服务
	 * @param ip 服务器ip地址   格式127.0.0.1  默认10s的延迟
	 * @return
	 */
	public boolean pingIp(String ip)
	{
		int timeout=10000;
		boolean r=false;
		try {
			r=InetAddress.getByName(ip).isReachable(timeout);
			log.info(ip+" available:"+r);
		} catch (UnknownHostException e) {
			log.error(ip+" 未知的主机地址");
		} catch (IOException e) {
			log.error(ip+" "+e.getMessage());
		}
		return r;
	}
}
